﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.IO;

namespace Config 
{
    public class Settings
    {
        private XmlDocument _xmlDoc = null;
        private XmlNode _xmlNode = null;
        private string _strFileName = "";
        private string _rootName = "omapldisplaysettings";
        private string path = "";
        public string Path
        {
            get { return path; }
            set { path = value; }
        }
        public omplconfig_detail ChannelConfig
        {
            get
            {
                XmlNode node = _xmlNode.SelectSingleNode("omplconfig_detail");
                if (node == null)
                    node = _xmlNode.AppendChild(_xmlDoc.CreateElement("omplconfig_detail"));
                return new omplconfig_detail(node);
            }
        }
        public omplconfig_sys SystemSet
        {
            get
            {
                XmlNode node = _xmlNode.SelectSingleNode("omplconfig_sys");
                if (node == null)
                    node = _xmlNode.AppendChild(_xmlDoc.CreateElement("omplconfig_sys"));
                return new omplconfig_sys(node);
            }
        }
        public omplconfig_store StoreSet
        {
            get
            {
                XmlNode node = _xmlNode.SelectSingleNode("omplconfig_store");
                if (node == null)
                    node = _xmlNode.AppendChild(_xmlDoc.CreateElement("omplconfig_store"));
                return new omplconfig_store(node);
            }
        }
        public Settings()
        {
            //_xmlNode = node;
            //_xmlDoc = new XmlDocument();
            //_xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "");
            //_xmlDoc.AppendChild(_xmlDoc.CreateElement(_rootName));
        }
        public void Create()
        {
            _xmlDoc = new XmlDocument();
            _xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", "");

            _xmlNode = _xmlDoc.AppendChild(_xmlDoc.CreateElement(_rootName));

        }
        
        public XmlNode GetChannel(int id)
        {
            string xmlPath = string.Format("omplconfig_detail/omaplchannels/channel[@NO=\"{0}\"]", id.ToString());
            XmlNode node = _xmlNode.SelectSingleNode(xmlPath);
            return node;
        }
        public bool Open(string strFileName)
        {
            bool bRet = true;
            try
            {
                _xmlDoc = new XmlDocument();
                _xmlDoc.Load(strFileName);
                _xmlNode = _xmlDoc.DocumentElement;
            }
            catch (System.Exception)
            {
                bRet = false;
            }

            _strFileName = strFileName;
            return bRet;
        }
        public void saveXml(string filepath)
        {
            try
            {
                if(_xmlDoc!=null)
                {
                    _xmlDoc.Save(filepath);
                }
            }
            catch(Exception ex)
            {
                
            }
        }
    }
}
